﻿
Imports System.IO
Imports System.Windows.Forms

Public Class MyLogging

    Public Shared Sub Message(Message As String, ListenerName As String, Optional WithDate As Boolean = False)
        '
        CreateListener(ListenerName)
        Dim Listener As TraceListener = Trace.Listeners.Item(ListenerName)
        If IsNothing(Listener) = False Then
            If WithDate Then
                Listener.WriteLine(Now & ": " & Message)
            Else
                Listener.WriteLine(Message)
            End If
            Listener.Flush()
        End If
        '
    End Sub
    Private Shared Sub CreateListener(ListenerName As String)
        If String.IsNullOrWhiteSpace(ListenerName) Then
            Throw New Exception("CreateListener:Name Der String ListenerName muss angegeben werden und darf nicht leer oder NULL sein.")
        End If
        Dim Listener As TraceListener = Trace.Listeners.Item(ListenerName)
        If IsNothing(Listener) Then
            Dim fi As New FileInfo(Application.StartupPath & "\Logs\" & ListenerName)
            If fi.Directory.Exists = False Then fi.Directory.Create() : fi.Refresh()
            Listener = New TextWriterTraceListener(fi.FullName, ListenerName)
            Trace.Listeners.Add(Listener)
        End If
    End Sub
    '
End Class


